//
//  YYNewCarVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/19.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYNewCarVC.h"
#import "YYbuyCarCell.h"
#import "YYCarDetailVC.h"
#import "PullView.h"
#import "YYSpellGroupDetailVC.h"

@interface YYNewCarVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (nonatomic ,strong)NSArray * dataArr;
@property (nonatomic ,strong)NSDictionary * dataDic;
@property (nonatomic ,assign)NSInteger lastTime;
@property (strong, nonatomic) SDCycleScrollView *adCycleScrollView;
@property (nonatomic,strong) NSDictionary * groupDict;
//@property (nonatomic,assign) BOOL hasGroup;
@property (nonatomic ,assign) CGFloat headNorH;
@property (nonatomic ,copy)NSString * moneyId;
@property (nonatomic ,copy)NSString * typeId;

@property (nonatomic ,strong)NSArray * shapeArr;
@property (nonatomic ,strong)NSArray * brandArr;
@property (nonatomic ,assign)NSInteger shapeIndex;
@property (nonatomic ,assign)NSInteger brandIndex;
@property (nonatomic ,assign)BOOL pull1;
@property (nonatomic ,assign)BOOL pull2;

@property (nonatomic,strong)UIView * footView;

@end

@implementation YYNewCarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.typeId = @"";
    self.moneyId = @"";
    self.shapeIndex = -1;
    self.brandIndex = -1;
    [self getCarList];
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [self getCarList];
    }];
    [self getData];
    [self setUpUI];
}
- (void)setUpUI{
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableHeaderView.height =194;
    self.labelViewHeight.constant = 1;
    self.groupHeight.constant = 0;
    self.labelView.hidden = YES;
    self.labelView.layer.borderWidth = 0.5;
    self.labelView.layer.borderColor = [UIColor colorWithHexString:@"bdbdbd"].CGColor;
    self.btnReset.layer.borderColor = [UIColor colorWithHexString:@"bdbdbd"].CGColor;
    self.btnReset.layer.borderWidth = 0.5;
    self.btnReset.layer.cornerRadius = 12;
    SDCycleScrollView *adCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:self.bannerView.bounds delegate:self placeholderImage:[UIImage new]];
    adCycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    adCycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleNone;
    adCycleScrollView.titleLabelTextAlignment = NSTextAlignmentRight;
    adCycleScrollView.contentMode = UIViewContentModeScaleAspectFill;
    [self.bannerView addSubview:adCycleScrollView];
    [adCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    self.adCycleScrollView = adCycleScrollView;
}

- (void)getCarList{
    [RequestOperationManager payCarList_type_id:self.typeId money_id:self.moneyId success:^(id result) {
        [self.tableView.mj_header endRefreshing];
        if (DEF_REQ_SUCCESS) {
            NSArray * list = result[@"data"][@"list"];
            self.shapeArr =  [result[@"data"][@"shape"] copy];
            self.brandArr = [result[@"data"][@"brand"] copy];
            self.dataArr = list;
            if (self.dataArr.count == 0) {
                self.tableView.tableFooterView = self.footView;
                
            }else{
                 self.tableView.tableFooterView = nil;

            }
            [self.tableView reloadData];
        }
        
    } failture:^(NSError *error) {
         [self.tableView.mj_header endRefreshing];
    }];
}

-(void)getData{
    [RequestOperationManager paycarBanner_success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            NSArray  * dataArr = result[@"data"];
            NSMutableArray * imageArr = [NSMutableArray arrayWithCapacity:0];
            for (int i =0; i<dataArr.count; i++) {
                [imageArr addObject:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,dataArr[i][@"image"]]];
            }
            self.adCycleScrollView.imageURLStringsGroup =imageArr;
        }
        
    } failture:^(NSError *error) {
        
    }];
    
    [RequestOperationManager paycarGroup_success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            self.groupDict = [result[@"data"] copy];
            if (![self.groupDict isKindOfClass:[NSNull class]] ) {
                self.tableView.tableHeaderView.height =340;
                self.labelViewHeight.constant = 1;
                self.groupHeight.constant = 146;
                self.groupView.hidden = NO;
               self.headNorH = 340;
                self.lbName.text = self.groupDict[@"brand"];
                [self.headerIcon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,self.groupDict[@"vehicle_img"]]]];
            }else{
                self.groupView.hidden = YES;
                self.tableView.tableHeaderView.height =194;
                self.labelViewHeight.constant = 1;
                self.groupHeight.constant = 0;
                self.headNorH = 194;
            }
        [self.tableView reloadData];
        }
        
    } failture:^(NSError *error) {
        
    }];
}

- (IBAction)priceClicked:(id)sender {
    if (self.shapeArr.count == 0) {
        return;
    }
    if (_pull1) {
        [self.btnPrice setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
        [[PullView sharedInstance] removeFromSuperview];
        _pull1 = NO;
        return;
    }
    
    [self.btnType setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
    [self.btnPrice setImage: DEF_IMAGENAME(@"Car_Select the button_chosed") forState:UIControlStateNormal];
    [PullView sharedInstance].selIndex = self.shapeIndex;
    MJWeakSelf;
    _pull1 = YES;
    _pull2 = NO;
    [PullView pullWithArr:self.shapeArr frme:CGRectMake(0,194, DEF_SCREEN_WIDTH(), 30) superView:self.tableView index:^(NSInteger index) {
        weakSelf.brandIndex = -1;
        weakSelf.typeId = @"";
        weakSelf.shapeIndex = index;
        weakSelf.moneyId = self.shapeArr[index][@"id"];
          weakSelf.lbTag.text = self.shapeArr[index][@"type"];
        weakSelf.labelView.hidden = NO;
        weakSelf.labelViewHeight.constant = 60;
        weakSelf.tableView.tableHeaderView.height = weakSelf.headNorH+60;
         [self.btnPrice setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
        [weakSelf getCarList];
    }];
}


- (IBAction)typeClicked:(id)sender {
    if (self.brandArr.count == 0) {
        return;
    }
    if (_pull2) {
        [self.btnType setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
        [[PullView sharedInstance] removeFromSuperview];
        _pull2 = NO;
        return;
    }
    
     [self.btnPrice setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
    [self.btnType setImage: DEF_IMAGENAME(@"Car_Select the button_chosed") forState:UIControlStateNormal];
    [PullView sharedInstance].selIndex = self.brandIndex;
     MJWeakSelf;
    _pull1 = NO;
    _pull2 = YES;
    [PullView pullWithArr:self.brandArr frme:CGRectMake(0,194, DEF_SCREEN_WIDTH(), 30) superView:self.tableView index:^(NSInteger index) {
          [self.btnType setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
        weakSelf.brandIndex = index;
        weakSelf.moneyId = @"";
        weakSelf.typeId = self.brandArr[index][@"id"];
        weakSelf.shapeIndex = -1;
         weakSelf.labelView.hidden = NO;
        weakSelf.labelViewHeight.constant = 60;
        weakSelf.tableView.tableHeaderView.height = weakSelf.headNorH+60;
        weakSelf.lbTag.text = self.brandArr[index][@"type"];
         [weakSelf getCarList];
    }];
}
- (IBAction)cancle:(id)sender {
    _pull1 = NO;
    _pull2 = NO;
    self.brandIndex = -1;
    self.shapeIndex = -1;
     self.labelView.hidden = YES;
    self.labelViewHeight.constant = 1;
    self.tableView.tableHeaderView.height = self.headNorH;
    self.moneyId = @"";
    self.typeId = @"";
    [self getCarList];
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self resetClicked:nil];
}

- (IBAction)resetClicked:(id)sender {
    _pull1 = NO;
    _pull2 = NO;
    self.brandIndex = -1;
    self.shapeIndex = -1;
    self.labelView.hidden = YES;
    self.labelViewHeight.constant = 1;
    self.tableView.tableHeaderView.height = self.headNorH;
    self.moneyId = @"";
    self.typeId = @"";
    [self.btnPrice setImage: DEF_IMAGENAME(@"Car_Select the button") forState:UIControlStateNormal];
    [self.btnType setImage: DEF_IMAGENAME(@"Car_Select the button")
     forState:UIControlStateNormal];
    [[PullView sharedInstance] removeFromSuperview];
    [self getCarList];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[PullView sharedInstance] removeFromSuperview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YYbuyCarCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YYbuyCarCell"];
    NSDictionary * itme = self.dataArr[indexPath.row];
    cell.lbName.text =[NSString stringWithFormat:@"%@ %@",itme[@"brand"],itme[@"vehicle_type"]];
    cell.lbShop.text = [NSString stringWithFormat:@"首付%@ 月供%@",itme[@"pay"],itme[@"monthly"]];
    cell.lbPrice.text = [NSString stringWithFormat:@"%@",itme[@"yes_money"]];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,itme[@"image"]]]];
    
    
    return cell;;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 136;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     NSDictionary * itme = self.dataArr[indexPath.row];
    YYCarDetailVC * detail = (YYCarDetailVC *)[CFUtility getVC:@"YYCarDetailVC" Storyboard:BUYCAR];;
    detail.carId = itme[@"id"];
    [self.navigationController pushViewController:detail animated:YES];
}

- (IBAction)toGroup:(id)sender {
    YYSpellGroupDetailVC * detail = (YYSpellGroupDetailVC *)[CFUtility getVC:@"YYSpellGroupDetailVC" Storyboard:BUYCAR];
    
    detail.carId = self.groupDict[@"id"];
    [self.navigationController pushViewController:detail animated:YES];
}


-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

-(UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH(), 200)];
        UIImageView * image = [[UIImageView alloc] init];
        [_footView addSubview:image];
        image.image = DEF_IMAGENAME(@"sign_no_information");
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(50);
        }];
        UILabel * titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.textColor = [UIColor colorWithHexString:@"999999"];
        titleLabel.text = @"暂无数据";
        [_footView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(image.mas_bottom).mas_offset(32);
        }];
    }
    return _footView;
}
@end
